﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;

namespace TCHSHue.BLL
{
    public class TaiLieuBLL
    {
        public static List<TaiLieu> getList()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.TaiLieus
                         orderby e.tenTaiLieu ascending
                         select e;
                return ls.ToList();
            }
        }
        public static List<TaiLieu> getList(string tenTaiLieu)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.TaiLieus
                         where e.tenTaiLieu.Contains(tenTaiLieu)
                         orderby e.tenTaiLieu ascending
                         select e;
                return ls.ToList();
            }
        }
        public static bool create(string tenTaiLieu, string url)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.TaiLieus
                          where e.tenTaiLieu == tenTaiLieu || e.url == url
                          select e).SingleOrDefault();
                if (ls != null)
                    return false;
                TaiLieu tl = new TaiLieu { tenTaiLieu = tenTaiLieu, url = url, ngayGoi = DateTime.Now.Date, maTaiLieu = Guid.NewGuid() };
                data.TaiLieus.InsertOnSubmit(tl);
                data.SubmitChanges();
                return true;
            }
        }

        internal static bool delete(string maTaiLieu)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                Guid mtl = new Guid(maTaiLieu);
                var tl = (from e in data.TaiLieus
                          where e.maTaiLieu == mtl
                          select e).SingleOrDefault();
                if (tl != null)
                {
                    data.TaiLieus.DeleteOnSubmit(tl);
                    data.SubmitChanges();
                    return true;
                }
                return false;
            }
        }

        internal static bool update(string maTaiLieu, string tenTaiLieu)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                try
                {
                    Guid mtl = new Guid(maTaiLieu);
                    var tl = (from e in data.TaiLieus
                              where e.maTaiLieu == mtl
                              select e).SingleOrDefault();
                    if (tl != null)
                    {
                        tl.tenTaiLieu = tenTaiLieu;
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static TaiLieu getTaiLieu(string maTaiLieu)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                try
                {
                    Guid mtl = new Guid(maTaiLieu);
                    var tl = (from e in data.TaiLieus
                              where e.maTaiLieu == mtl
                              select e).SingleOrDefault();
                    return tl;
                }
                catch
                {
                    return null;
                }
            }
        }
    }
}